home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / pasclern.zip / CHAP11.TXT < prev    next >
Text File  |  1993-04-01  |  26KB  |  587 lines

  1.                            CHAPTER 11 - Files
  2.  
  3.  
  4.             One  of the most common operations when using a computer 
  5.         is to either read from, or write to a file.  You are already 
  6.         somewhat experienced in file handling from the last chapter, 
  7.         because in computer terminology, the keyboard, terminal, and 
  8.         printer are all classified as files.   A file is any  serial 
  9.         input  or  output  device that the computer has  access  to.  
  10.         Since  it  is  serial,  only one  piece  of  information  is 
  11.         available to the computer at any instant of time. This is in 
  12.         contrast to an array,  for example, in which all elements of 
  13.         the array are stored internally and are all available at any 
  14.         time.
  15.  
  16.             Several  years  ago computers were all large  cumbersome 
  17.         machines with large peripheral devices such as magnetic tape 
  18.         drives,  punch card readers,  paper tape readers or punches, 
  19.         etc.  It was a simple task to assign the paper tape reader a 
  20.         symbol and use that symbol whenever it was necessary to read 
  21.         a  paper tape.   There was never more than one file  on  the 
  22.         paper  tape being read,  so it was simply read sequentially, 
  23.         and  hopefully  the data was the  desired  data.   With  the 
  24.         advent  of  floppy  disks,  and hard disks  too,  it  became 
  25.         practical to put several files of data on one disk,  none of 
  26.         which  necessarily had anything to do with any of the  other 
  27.         files on that disk.   This led to the problem of reading the 
  28.         proper file from the disk, not just reading the disk.
  29.  
  30.             Pascal  was  originally released  in  1971,  before  the 
  31.         introduction  of  the  compact floppy  disk.   The  original 
  32.         release  of Pascal had no provision for selecting a  certain 
  33.         file  from  among  the many  included  on  the  disk.   Each 
  34.         compiler  writer had to overcome this deficiency and he  did 
  35.         so  by defining an extension to the standard Pascal  system.  
  36.         Unfortunately,  all of the extensions were not the same, and 
  37.         there  are  now several ways to accomplish  this  operation.  
  38.         There   are  primarily  two  ways,   one  using  the  ASSIGN 
  39.         statement, and the other using the OPEN statement.  They are 
  40.         similar  to  each  other and they accomplish  the  same  end 
  41.         result.
  42.  
  43.             All  of the above was described to let you know that  we 
  44.         will have a problem in this chapter, namely, how do we cover 
  45.         all  of  the possible implementations of  Pascal  available?  
  46.         The answer is,  we can't.   Most of what is covered in  this 
  47.         chapter will apply to all compilers, and all that is covered 
  48.         will  apply to the TURBO Pascal compiler.   If your compiler 
  49.         complains about some of the statements, it will be up to you 
  50.         to dig out the details of how to do the intended operations.  
  51.         If there is no way to do any of these operations, you should 
  52.         seriously  consider getting another compiler because all  of 
  53.         these operations are needed in a useful Pascal environment.
  54.  
  55.  
  56.  
  57.                                  Page 51
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                            CHAPTER 11 - Files
  68.  
  69.  
  70.                        READING AND DISPLAYING A FILE
  71.  
  72.             Examine  the file READFILE for an example of  a  program 
  73.         that  can  read a text file from the disk,  in fact it  will 
  74.         read  itself  from  the disk and display  it  on  the  video 
  75.         monitor.   The  first statement in the program is the ASSIGN 
  76.         statement.   This  is TURBO Pascal's way of selecting  which 
  77.         file on the disk will be either read from or written to.  In 
  78.         this case we will read from the disk.  The first argument in 
  79.         the  ASSIGN  statement is the device  specifier  similar  to 
  80.         "lst"  used  in the last chapter for the printer.   We  have 
  81.         chosen  to  use  "turkey",  but could have  used  any  valid 
  82.         identifier.   This  identifier  must  be defined  in  a  VAR 
  83.         declaration as a TEXT type variable.   The next argument  is 
  84.         the  filename  desired.   The filename can be defined  as  a 
  85.         string constant, as it is here, or as a string variable.
  86.  
  87.             The TEXT type is a predefined type and is used to define 
  88.         a file identifier.  It is predefined as a "file of CHAR", so 
  89.         it can only be used for a text file.  We will see later that 
  90.         there is another type of file, a binary file.
  91.  
  92.             Now  that we have a file identified,  it is necessary to 
  93.         prepare it for reading by executing a RESET statement.   The 
  94.         reset statement positions the read pointer at the  beginning 
  95.         of  the file ready to read the first piece of information in 
  96.         the  file.   Once we have done that,  data is read from  the 
  97.         file  in  the same manner as it was when  reading  from  the 
  98.         keyboard.   In this program,  the input is controlled by the 
  99.         WHILE  loop  which is executed until we exhaust the data  in 
  100.         the file.
  101.  
  102.                  WHAT ARE THE "EOF" AND "EOLN" FUNCTIONS?
  103.  
  104.             The "eof" function is new and must be defined.   When we 
  105.         read  data from the file,  we move closer and closer to  the 
  106.         end,  until  finally we reach the end and there is  no  more 
  107.         data  to  read.   This  is  called  "end  of  file"  and  is 
  108.         abbreviated "eof".   Pascal has this function which is false 
  109.         until we reach the last line of the file,  but when there is 
  110.         no  more  data in the file to be read,  the  function  "eof" 
  111.         becomes  true.   To use the function,  we merely give it our 
  112.         file identifier as an argument.   It should be clear that we 
  113.         will  loop  until we read all of the data available  in  the 
  114.         file.
  115.  
  116.             The "eoln" function is not used in this program but is a 
  117.         very useful function.   If the input pointer is anywhere  in 
  118.         the  text  file  except at the end of  a  line,  the  "eoln" 
  119.         function  is  false,  but at the end of a line,  it  becomes 
  120.         true.   This function can therefore be used to find the  end 
  121.  
  122.  
  123.                                  Page 52
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                            CHAPTER 11 - Files
  134.  
  135.  
  136.         of  a  line  of text for variable length  text  input.   The 
  137.         "eoln"  function is not available,  and in fact  meaningless 
  138.         when you are reading a binary file, to be defined later.
  139.  
  140.             To actually read the data,  we use the READLN procedure, 
  141.         giving  it  our  identifier "turkey" and  the  name  of  the 
  142.         variable we want the data read into.   In this case, we read 
  143.         up  to  80  characters  into  the string  and  if  more  are 
  144.         available,  ignore  them.   Remember this from the  keyboard 
  145.         input?  It  is  the same here.   Since we would like  to  do 
  146.         something  with the data,  we simply output the line to  the 
  147.         default device,  the video monitor.   It should be clear  to 
  148.         you by now that the program will simply read the entire file 
  149.         and display it on the monitor.
  150.  
  151.             Finally,  we CLOSE the file "turkey".   It is not really 
  152.         necessary to close the file because the system will close it 
  153.         for  you automatically at program termination,  but it is  a 
  154.         good  habit to get into.   It must be carefully pointed  out 
  155.         here,  that  you did not do anything to the input file,  you 
  156.         only  read it and left it intact.   You could RESET  it  and 
  157.         reread it again in this same program.   Compile and run this 
  158.         program to see if it does what you expect it to do.
  159.  
  160.                         A PROGRAM TO READ ANY FILE
  161.  
  162.             Examine  the next program READDISP for an improved  file 
  163.         reading  program.   This is very similar except that it asks 
  164.         you for the name of the file that you desire to display, and 
  165.         enters   the   name  into  a  12  character   string   named 
  166.         "name_of_file_to_input".   This  is then used in the  ASSIGN 
  167.         statement  to select the file to be read,  and the  file  is 
  168.         reset  as  before.   A  header is then  displayed,  and  the 
  169.         program  is  identical  to  the last  one  with  some  small 
  170.         additions.   In  order to demonstrate the use of a  function 
  171.         within the WRITELN specification,  the program calls for the 
  172.         length of the input string and displays it before each line.  
  173.         The  lines are counted as they are read and  displayed,  and 
  174.         the line count is then displayed at the end of the  listing.  
  175.         You  should  be  able  to  see clearly  how  each  of  these 
  176.         operations is accomplished.   Compile and run this  program, 
  177.         entering  any  filename  we  have used so far  (be  sure  to 
  178.         include  the  .PAS).    After  a  successful  run,  enter  a 
  179.         nonexistent filename and see the I/O error.
  180.  
  181.                        HOW TO COPY A FILE (SORT OF)
  182.  
  183.             Examine the file READSTOR for an example of both reading 
  184.         from a file and writing to another one.   In this program we 
  185.         request  an operator input for the filename to  read,  after 
  186.         which we ASSIGN the name to the file and RESET it.  Next, we 
  187.  
  188.  
  189.                                  Page 53
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                            CHAPTER 11 - Files
  200.  
  201.  
  202.         request a different filename to write to,  which is assigned 
  203.         to a different identifier.  The next statement is new to us, 
  204.         the REWRITE statement.   This name apparently comes from the 
  205.         words  REset  for WRITEing because that is exactly  what  it 
  206.         does.   It  clears  the entire file of any  prior  data  and 
  207.         prepares to write into the very beginning of the file.  Each 
  208.         time you write into it,  the file grows by the amount of the 
  209.         data written.
  210.  
  211.             Once  the identifier has been defined,  and the  REWRITE 
  212.         has  been  executed,  writing  to the file is  identical  to 
  213.         writing  to the display with the addition of the  identifier 
  214.         being specified before the first output field.  With that in 
  215.         mind, you should have no trouble comprehending the operation 
  216.         of the program.   It is similar to the last program,  except 
  217.         that  it  numbers the lines as the file  is  copied.   After 
  218.         running  the  program,  look on your default  disk  for  the 
  219.         filename  you  input when it asked for the output  filename.  
  220.         Examine that file to see if it is truly a copy of the  input 
  221.         file with line numbers added.   One word of caution,  if you 
  222.         used an existing filename for the output file,  the file was 
  223.         overwritten,  and the original destroyed.   In that case, it 
  224.         was  good that you followed instructions at the beginning of 
  225.         this  tutorial and made a working copy.   You did  do  that, 
  226.         didn't you?
  227.  
  228.                    HOW TO READ INTEGER DATA FROM A FILE
  229.  
  230.             It is well and good to be able to read text from a file, 
  231.         but now we come to the time to read data from a file.  First 
  232.         we will read data from a text file, then later from a binary 
  233.         file.   Examine  the  program  READINTS for  an  example  of 
  234.         reading data from a text file.  A text file is an ASCII file 
  235.         that can be read by a text editor, printed, displayed, or in 
  236.         some cases, compiled and executed.  It is simply a file made 
  237.         up of a long string of CHAR type data,  and usually includes 
  238.         linefeeds, carriage returns, and blanks for neat formatting.  
  239.         Nearly  every  file on the Tutorial disk you  received  with 
  240.         this  package is a text file.   The notable exception is the 
  241.         file named LIST.COM, which is an executable program file.
  242.  
  243.             The  example  program has nothing  new,  you  have  seen 
  244.         everything in it before.  We have an assignment, followed by 
  245.         a reset of our file,  followed by four read and write loops.  
  246.         Each  of the loops has a subtle difference to illustrate the 
  247.         READ  and READLN statements.   Notice that the same file  is 
  248.         read in four times with a RESET prior to each,  illustrating 
  249.         the nondestructive read mentioned a few paragraphs ago.
  250.  
  251.             The file we will be using is named INTDATA.TXT and is on 
  252.         your  disk.   You  could display it at this time  using  the 
  253.  
  254.  
  255.                                  Page 54
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                            CHAPTER 11 - Files
  266.  
  267.  
  268.         program  READDISP  we covered recently.   Notice that it  is 
  269.         simply  composed  of  the integer values  from  101  to  148 
  270.         arranged four to a line with a couple of spaces between each 
  271.         for  separation and a neat appearance.   The important thing 
  272.         to remember is that there are four data points per line.
  273.  
  274.                   READ AND READLN ARE SLIGHTLY DIFFERENT
  275.  
  276.             As  variables  are read in with  either  procedure,  the 
  277.         input  file  is scanned for the variables  using  blanks  as 
  278.         delimiters.  If there are not enough data points on one line 
  279.         to satisfy the arguments in the input list, the next line is 
  280.         searched also,  and the next,  etc.  Finally when all of the 
  281.         arguments  in  the  input list are satisfied,  the  READ  is 
  282.         complete, but the READLN is not.  If it is a READ procedure, 
  283.         the input pointer is left at that point in the file,  but if 
  284.         it is a READLN procedure,  the input pointer is advanced  to 
  285.         the  beginning of the next line.   The next paragraph should 
  286.         clear that up for you.
  287.  
  288.             The input data file INTDATA.TXT has four data points per 
  289.         line but the first loop in the program READINTS.PAS requests 
  290.         only  three  each time through the  loop.   The  first  time 
  291.         through, it reads the values 101, 102, and 103, and displays 
  292.         those  values,  leaving the input pointer just prior to  the 
  293.         104, because it is a READ procedure.  The next time through, 
  294.         it reads the value 104,  advances to the next line and reads 
  295.         the values 105,  and 106,  leaving the pointer just prior to 
  296.         the 107.  This continues until the 5 passes through the loop 
  297.         are completed.
  298.  
  299.             The next loop contains a READLN procedure and also reads 
  300.         the values 101,  102,  and 103, but when the input parameter 
  301.         list is satisfied,  it moves the pointer to the beginning of 
  302.         the next line,  leaving it just before the 105.   The values 
  303.         are printed out and the next time we come to the READLN,  we 
  304.         read the 105,  106, and 107, and the pointer is moved to the 
  305.         beginning  of  the next line.   It would be good to run  the 
  306.         program now to see the difference in output data for the two 
  307.         loops.
  308.  
  309.             When you come back to the program again, notice the last 
  310.         two loops, which operate much like the first two except that 
  311.         there  are  now five requested integer  variables,  and  the 
  312.         input  file  still  only has four  per  line.   This  is  no 
  313.         problem.   Both  input procedures will simply read the first 
  314.         four in the first line,  advance to the second line for  its 
  315.         required  fifth  input,  and each will do its own  operation 
  316.         next.   The READ procedure will leave the input pointer just 
  317.         before  the second data point of the second  line,  and  the 
  318.         READLN  will  advance the input pointer to the beginning  of 
  319.  
  320.  
  321.                                  Page 55
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                            CHAPTER 11 - Files
  332.  
  333.  
  334.         the  third  line.   Run this program and  observe  the  four 
  335.         output fields to see an illustration of these principles.
  336.  
  337.                 NOW TO READ SOME REAL VARIABLES FROM A FILE
  338.  
  339.             By  whatever method you desire,  take a look at the file 
  340.         named  REALDATA.TXT supplied on your Pascal  Tutorial  disk.  
  341.         You  will see 8 lines of what appears to be scrambled  data, 
  342.         but it is good data that Pascal can read.  Notice especially 
  343.         line  4  which has some data missing,  and line 6 which  has 
  344.         some extra data.
  345.  
  346.             Examine the program file READDATA which will be used  to 
  347.         illustrate  the  method of reading  REAL  data.   Everything 
  348.         should be familiar to you,  since there is nothing new here.  
  349.         Notice  the READLN statement.  It is requesting one  integer 
  350.         variable,  and  three real variables,  which is what most of 
  351.         the input file contained.   When we come to the fourth line, 
  352.         there are not enough data points available, so the first two 
  353.         data points of the next line are read to complete the fourth 
  354.         pass.  Since the pointer is advanced to the beginning of the 
  355.         next line,  we are automatically synchronized with the  data 
  356.         again.   When  we come to the sixth line,  the last two data 
  357.         points  are simply ignored.   Run the program to see if  the 
  358.         results are as you would predict.
  359.  
  360.             If a READ were substituted for the READLN,  the  pointer 
  361.         would not be advanced to the beginning of line 6,  after the 
  362.         fourth  pass  through the loop.   The next attempt  to  read 
  363.         would result in trying to read the .0006 as an INTEGER,  and 
  364.         a  run time error would result.   Modify the program and see 
  365.         if this is not true.
  366.  
  367.             That is all there is to reading and writing text  files.  
  368.         If  you  learn the necessities,  you will not  be  stumbling 
  369.         around   in   the  area  of  input/output  which   is   very 
  370.         intimidating to many people.  Remember to ASSIGN, then RESET 
  371.         before  reading,  REWRITE before writing,  and CLOSE  before 
  372.         quitting.   It  is of the utmost importance to close a  file 
  373.         you  have been writing to before quitting to write the  last 
  374.         few  buffers to the file,  but it is not important to  close 
  375.         read  files unless you are using a lot of them,  as there is 
  376.         an  implementation dependent limit of how many files can  be 
  377.         open at once.  It is possible to read from a file, close it, 
  378.         reopen it,  and write in it in one program.  You can reuse a 
  379.         file  as often as you desire in a program,  but  you  cannot 
  380.         read from and write into a file at the same time.
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.                                  Page 56
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                            CHAPTER 11 - Files
  398.  
  399.  
  400.                       NOW FOR BINARY INPUT AND OUTPUT
  401.  
  402.             Examine  the file BINOUT for an example of writing  data 
  403.         to a file in binary form.   First there is a record  defined 
  404.         in  the  type declaration part composed of  three  different 
  405.         variable types.   In the VAR part,  "output_file" is defined 
  406.         as  a "FILE of dat_rec",  the record defined  earlier.   The 
  407.         variable  "dog_food"  is  then defined as an  array  of  the 
  408.         record, and a simple variable is defined.
  409.  
  410.             Any  file assigned a type of TEXT,  which is a "FILE  of 
  411.         CHAR", is a text file.  A text file can be read and modified 
  412.         with a text editor,  printed out,  displayed on the monitor, 
  413.         etc. If a file is defined with any other definition, it will 
  414.         be  a  binary  file  and will be in an  internal  format  as 
  415.         defined by the Pascal compiler.   Attempting to display such 
  416.         a file will result in very strange looking gibberish on  the 
  417.         monitor.
  418.  
  419.             When we get to the program,  the output file is assigned 
  420.         a name,  and a REWRITE is performed on it to reset the input 
  421.         point  to  the beginning of the file,  empty the  file,  and 
  422.         prepare  for  writing data into it.   The next  loop  simply 
  423.         assigns  nonsense  data to all of the variables  in  the  20 
  424.         records so we have something to work with.
  425.  
  426.             We  finally write a message to the display that  we  are 
  427.         ready  to start outputting data,  and we output the data one 
  428.         record at a time with the standard WRITE statement.   A  few 
  429.         cautions are in order here.   The output file can be defined 
  430.         as  any simple variable type,  INTEGER,  BYTE,  REAL,  or  a 
  431.         record, but cannot be mixed.  The record however, can be any 
  432.         combination of data including other records, if desired, but 
  433.         any  file  can only have one type of record written  to  it.  
  434.         Also,  a  WRITELN  statement  is illegal when writing  to  a 
  435.         binary file because a binary file is not line  oriented.   A 
  436.         WRITE   statement  is  limited  to  one  output  field   per 
  437.         statement.  It is a simple matter to put one WRITE statement 
  438.         in  the  program for each variable you wish to write out  to 
  439.         the  file.   It is important to CLOSE the file when you  are 
  440.         finished writing to it.
  441.  
  442.                            WHY USE A BINARY FILE
  443.  
  444.             A binary file written by a Pascal program cannot be read 
  445.         by a word processor,  a text editor, any application program 
  446.         such  as a database or spreadsheet,  and it may not even  be 
  447.         readable  by  a  Pascal  program  compiled  by  a  different 
  448.         companies  compiler  because  the  data  is   implementation 
  449.         dependent.   It can't even be read by a Pascal program using 
  450.         the  correct compiler unless the data structure is identical 
  451.  
  452.  
  453.                                  Page 57
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.                            CHAPTER 11 - Files
  464.  
  465.  
  466.         to the one used to write the file.  With all these rules, it 
  467.         seems  like  a  silly way to  output  data,  but  there  are 
  468.         advantages to using a binary output.
  469.  
  470.             A  binary file uses less file space than a corresponding 
  471.         text  file  because  the data is stored in  a  packed  mode.  
  472.         Since all significant digits of REAL data are stored,  it is 
  473.         more   precise  unless  you  are  careful  to   output   all 
  474.         significant  data to the corresponding TEXT file.   Finally, 
  475.         since the binary data does not require formatting into ASCII 
  476.         characters,  it will be considerably faster than  outputting 
  477.         it  in TEXT format.   When you run the example  program,  it 
  478.         will create the file KIBBLES.BIT,  and put 20 records in it.  
  479.         Return  to  DOS  and  look  for this  file  and  verify  its 
  480.         existence.   If  you try to TYPE it,  you will have  a  real 
  481.         mess, but that might be a good exercise.
  482.  
  483.                            READING A BINARY FILE
  484.  
  485.             BININ  is another example program that will read in  the 
  486.         file  we just created.   Notice that the variables are named 
  487.         differently,  but the types are all identical to those  used 
  488.         to  write  the  file.   An additional line is found  in  the 
  489.         program,  the IF statement.   We must check for the "end  of 
  490.         file"  marker to stop reading when we find it or Pascal will 
  491.         list  an  error and terminate operation.   Three  pieces  of 
  492.         information  are written out to verify that we actually  did 
  493.         read the data file in.
  494.  
  495.             Once  again,  a  few rules are in order.   A  READLN  is 
  496.         illegal since there are no lines in a binary file,  and only 
  497.         one variable or record can be read in with a READ statement.
  498.  
  499.             WHAT ABOUT FILE POINTERS, GET, AND PUT STATEMENTS?
  500.  
  501.             File pointers and the GET and PUT procedures are a  part 
  502.         of standard Pascal,  but since they are redundant,  they are 
  503.         not  a  part of TURBO Pascal.   The standard READ and  WRITE 
  504.         procedures are more flexible,  more efficient, and easier to 
  505.         use.   The  use  of GET and PUT will not be  illustrated  or 
  506.         defined here.  If you ever have any need for them, they will 
  507.         be covered in detail in your Pascal reference manual for the 
  508.         particular implementation you are using.
  509.  
  510.             Pointers  will be covered in detail in the next  chapter 
  511.         of this tutorial.
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.                                  Page 58
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.                            CHAPTER 11 - Files
  530.  
  531.  
  532.                            PROGRAMMING EXERCISES
  533.  
  534.         1.  Write a program to read in any text file, and display it 
  535.             on  the  monitor  with line numbers and  the  number  of 
  536.             characters  in each line.  Finally display the number of 
  537.             lines  found  in  the file,  and  the  total  number  of 
  538.             characters in the entire file.  Compare this number with 
  539.             the filesize given by the DOS command DIR.
  540.  
  541.         2.  Write  a silly program that will read two text files and 
  542.             display  them both on the monitor on alternating  lines. 
  543.             This is the same as "shuffling" the two files  together. 
  544.             Take  care  to  allow them to end  at  different  times, 
  545.             inserting  blank  lines  for the  file  that  terminates 
  546.             earlier.
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.                                  Page 59
  586.  
  587.